#!/bin/bash

sleep 15

cd `dirname $0`
BIN_DIR=`pwd`
cd ..
DEPLOY_DIR=`pwd`
CONF_DIR=$DEPLOY_DIR/conf

if [ ! -n "$ZK_HOST" ]; then
    echo "ZK_HOST not exist!"
    exit 1
fi

if [ ! -n "$ZK_PORT" ]; then
    echo "ZK_PORT not exist!"
    exit 1
fi


sed -i "s/ZK_HOST/$ZK_HOST/" /app/apache-tomcat/webapps/ROOT/WEB-INF/dubbo.properties
sed -i "s/ZK_PORT/$ZK_PORT/" /app/apache-tomcat/webapps/ROOT/WEB-INF/dubbo.properties
sed -i "s/8080/5000/" /app/apache-tomcat/conf/server.xml

msg="memory too low, can't start server，need memory between 2G and 8G"

MEM_OPTS=""
if [[ $MEMORY_SIZE = "2xlarge" ]];then
    MEM_OPTS="-Xms2048m -Xmx2048m -XX:NewSize=256m -XX:PermSize=128m -server"
elif [[ $MEMORY_SIZE = "4xlarge" ]];then
    MEM_OPTS="-Xms4096m -Xmx4096m -XX:NewSize=512m -XX:PermSize=256m -server"
elif [[ $MEMORY_SIZE = "8xlarge" ]];then
    MEM_OPTS="-Xms8192m -Xmx8192m -XX:NewSize=1024m -XX:PermSize=512m -server"
elif [[ $MEMORY_SIZE = "16xlarge" ]];then
    MEM_OPTS="-Xms10240m -Xmx10240m -XX:NewSize=2048m -XX:PermSize=1024m -server"
elif [[ $MEMORY_SIZE = "32xlarge" ]];then
    MEM_OPTS="-Xms10240m -Xmx10240m -XX:NewSize=2048m -XX:PermSize=1024m -server"
elif [[ $MEMORY_SIZE = "64xlarge" ]];then
    MEM_OPTS="-Xms10240m -Xmx10240m -XX:NewSize=2048m -XX:PermSize=1024m -server"
fi

ulimit -n 102400

/app/apache-tomcat/bin/catalina.sh run
